
#include "turboscreen.h"
#include <QRect>

TurboScreen::TurboScreen(QObject *parent)
    : QObject(parent)
{

}

TurboScreen::TurboScreen(const QSize &size, QObject *parent)
    : QObject(parent)
{
    max_width_       = size.width();
    max_height_      = size.height();
    mouse_start_pos_ = QPoint(-1, -1);
    mouse_end_pos_   = mouse_start_pos_;
    pos_left_up_     = mouse_start_pos_;
    pos_right_down_  = mouse_start_pos_;
    status_ = emSELECT;
}

TurboScreen::~TurboScreen()
{

}

void TurboScreen::setPosLeftUp(const QPoint &point)
{
    pos_left_up_ = point;
}

QPoint TurboScreen::getPosLeftUp()
{
    return pos_left_up_;
}

void TurboScreen::setPosRightDown(const QPoint &point)
{
    pos_right_down_ = point;
}

QPoint TurboScreen::getPosRightDown()
{
    return pos_right_down_;
}

void TurboScreen::setMouseStartPos(const QPoint &point)
{
    mouse_start_pos_ = point;
}

QPoint TurboScreen::getMouseStartPos()
{
    return mouse_start_pos_;
}

void TurboScreen::setMouseEndPos(const QPoint &point)
{
    mouse_end_pos_ = point;
    pos_left_up_ = mouse_start_pos_;
    pos_right_down_ = mouse_end_pos_;
    comparePoints(pos_left_up_, pos_right_down_);
}

QPoint TurboScreen::getMouseEndPos()
{
    return mouse_end_pos_;
}

void TurboScreen::setMaxWidth(const int &value)
{
    max_width_ = value;
}

int TurboScreen::getMaxWidth()
{
    return max_width_;
}

void TurboScreen::setMaxHeight(const int &value)
{
    max_height_ = value;
}

int TurboScreen::getMaxHeight()
{
    return max_height_;
}

TurboScreen::STATUS TurboScreen::getStatus()
{
    return status_;
}

void TurboScreen::setStatus(const TurboScreen::STATUS &status)
{
    status_ = status;
}

bool TurboScreen::checkIsInArea(const QPoint &pos)
{
    QRect rect(pos_left_up_,pos_right_down_);
    return rect.contains(pos);
}

void TurboScreen::move(const QPoint &p)
{
    int lx = pos_left_up_.x() + p.x();
    int ly = pos_left_up_.y() + p.y();
    int rx = pos_right_down_.x() + p.x();
    int ry = pos_right_down_.y() + p.y();

    if (lx < 0) {
        lx = 0;
        rx -= p.x();
    }
    if (ly < 0) {
        ly = 0;
        ry -= p.y();
    }
    if (rx > max_width_)
    {
        rx = max_width_;
        lx -= p.x();
    }
    if (ry > max_height_) {
        ry = max_height_;
        ly -= p.y();
    }
    pos_left_up_     = QPoint(lx, ly);
    pos_right_down_  = QPoint(rx, ry);
    mouse_start_pos_ = pos_left_up_;
    mouse_end_pos_   = pos_right_down_;
}

void TurboScreen::comparePoints(QPoint &leftTop, QPoint &rightDown)
{
    QPoint l = leftTop;
    QPoint r = rightDown;
    if (l.x() <= r.x()) {
        if (l.y() <= r.y()) {
            ;
        } else {
            leftTop.setY(r.y());
            rightDown.setY(l.y());
        }
    } else {
        if (l.y() < r.y()) {
            leftTop.setX(r.x());
            rightDown.setX(l.x());
        } else {
            QPoint tmp;
            tmp = leftTop;
            leftTop = rightDown;
            rightDown = tmp;
        }
    }
}
